package hive;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with hadoop-test.
 * 作者: 周艳钢
 * Date: 2016/11/9 0009
 * Time: 下午 5:14
 * 功能：
 */
@Description(name="zodiac",
    value = "_FUNC_(date) - from the input date string " +
    "or separate month and day arguments, returns the sign of the Zodiac.",
    extended = "Example: \n"
        + " > SELECT _FUNC_(date_string) FROM src;\n"
        + " > SELECT _FUNC_(month, day) FROM src;")
public class HiveMyUDF extends UDF{

    private SimpleDateFormat dateFormat;

    public HiveMyUDF() {
        this.dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    }

    public String evaluate(Date bday) {
        return this.evaluate(bday.getMonth(), bday.getDate());
    }

    public String evaluate(String bday) {
        Date date = null;
        try {
            date = dateFormat.parse(bday);
        } catch (Exception ex) {
            return null;
        }

        return this.evaluate(date.getMonth() + 1, date.getDate());
    }

    public String evaluate(Integer month, Integer day) {
        if (month == 1) {
            if (day < 20) {
                return "Capricorn";
            } else {
                return "Aquarius";
            }
        }

        if (month == 2) {
            if (day < 19) {
                return "Aquarius";
            } else {
                return "Pisces";
            }
        }


        return "zhouyg";

    }
}
